//获取应用实例
var app = getApp();
Page({
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        
        wxcode:""
    },
    onShow:function(){
    },
    onLoad: function (options) {
      //获取手机高度
      this.setData({
        navH: app.globalData.navHeight
      })
      var that = this;
      this.setData({
        partnerUserId : options.partnerUserId,
        assem: options.assem,
        orderId: options.orderId,
        share: options.share,
        type: options.type,
        productid: options.productid,
        groupShoppingId: options.groupShoppingId,
        giving: options.giving,
        usergiftcardid: options.usergiftcardid,
        partnerUserName: options.partnerUserName,
        coupon: options.coupon,
        cardid: options.cardid,
        price: options.price,
        shareBack: options.shareBack,
        fan: options.fan
      })
      if (options.usergiftcardid == undefined) {
        that.setData({
          usergiftcardid: 0,
        })
      }
    },
    newGetInfo: function (e) {
      var that = this;
      wx.getUserProfile({
        desc: '获得你的公开信息(昵称，头像等)', //不写不弹提示框
        success: function (res) {
            //获取用户敏感数据密文和偏移向量
            that.setData({ encryptedData: res.encryptedData });
            that.setData({ iv: res.iv });
            wx.login({
              success: function (r) {
                 //微信js_code
                 that.setData({ wxcode: r.code });
                  wx.request({
                    url: app.globalData.url+'getUserWXLoginInfo',
                    data:{
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      code: that.data.wxcode,
                      partnerUserId: that.data.partnerUserId,
                      cardId: that.data.usergiftcardid 
                    },
                    success: function (res) {
                      var data = res.data;
                      if (data.result==1) {
                        //wx.setStorageSync("data", data.data);
                        wx.setStorageSync("userId", data.data.userId);
                        wx.setStorageSync("nickName", data.data.nickName);
                        wx.setStorageSync("avatar", data.data.avatar);
                        wx.setStorageSync("applicationType", data.data.applicationType);
                        wx.setStorageSync("authStatus", data.data.authStatus);
                        wx.setStorageSync("balance", data.data.balance);
                        wx.setStorageSync("boundWechat", data.data.boundWechat);
                        wx.setStorageSync("commissionMoneyAmount", data.data.commissionMoneyAmount);
                        wx.setStorageSync("completeNumber", data.data.completeNumber);
                        wx.setStorageSync("createTime", data.data.createTime);
                        wx.setStorageSync("invitationCode", data.data.invitationCode);
                        wx.setStorageSync("inviteUserId", data.data.inviteUserId);
                        wx.setStorageSync("parentUserId", data.data.parentUserId);
                        wx.setStorageSync("partner", data.data.partner);
                        wx.setStorageSync("level", data.data.level);
                        wx.setStorageSync("partnerStatus", data.data.partnerStatus);
                        wx.setStorageSync("points", data.data.points);
                        wx.setStorageSync("promoteUserNumber", data.data.promoteUserNumber);
                        wx.setStorageSync("roleId", data.data.roleId);
                        wx.setStorageSync("sex", data.data.sex);
                        wx.setStorageSync("withdrawWay", data.data.withdrawWay);
                        wx.setStorageSync("wechatUnoinId", data.data.wechatUnoinId);
                        wx.setStorageSync("ongoing", data.data.ongoing);
                        wx.setStorageSync("registration", data.data.registration);
                        wx.setStorageSync("completed", data.data.completed);
                        wx.setStorageSync("token", data.data.token);
                        wx.setStorageSync("invitationPrice", data.data.invitationPrice);
                        wx.setStorageSync("receive", data.data.receive);
                        //wx.setStorageSync("pickUpCode", data.data.pickUpCode);
                          if (that.data.assem == 1) {
                            wx.navigateTo({
                              url: '/pages/assembleDetail/assembleDetail?orderId=' + that.data.orderId + '&share=' + that.data.share + '&type=' + that.data.type + '&groupShoppingId=' + that.data.groupShoppingId+'&shareBack=' + that.data.shareBack,
                            })
                          } else if (that.data.productid != 0 && that.data.productid != undefined && that.data.productid != '') {
                            wx.navigateTo({
                              url: '/pages/productDetail/productDetail?productid=' + that.data.productid + '&type=' + that.data.type + '&back=1',
                            })

                        } else  if (that.data.giving == 1 || that.data.coupon == 1) { 
                            wx.setStorageSync("partnerUserId", that.data.partnerUserId);
                            wx.setStorageSync("giving", that.data.giving); 
                            wx.setStorageSync("usergiftcardid", that.data.usergiftcardid);
                            wx.setStorageSync("partnerUserName", that.data.partnerUserName);
                            console.info(that.data.partnerUserName);
                            wx.reLaunch({
                              url: '/pages/mallPage/mallPage'
                            });
                          } else if (that.data.giving == 2) {
                            wx.setStorageSync("partnerUserId", that.data.partnerUserId);
                            wx.setStorageSync("giving", that.data.giving);
                            wx.setStorageSync("cardid", that.data.cardid);
                            wx.setStorageSync("partnerUserName", that.data.partnerUserName);
                            wx.setStorageSync("price", that.data.price);
                            console.info(that.data.partnerUserName);
                            wx.reLaunch({
                              url: '/pages/mallPage/mallPage'
                            });
                          } else if (that.data.fan != undefined) {
                            wx.navigateBack({
                              
                            })
                          } else  {
                            
                            wx.reLaunch({
                              url: '/pages/mallPage/mallPage' 
                            });
                        }
                      } else {
                        wx.showToast({
                          title: res.data.errmsg,
                          icon: 'none'
                        })
                      }
                    }
                  }) 
              }
            })
 
        },
        fail: function (err) {
          wx.reLaunch({
            url: '/pages/mallPage/mallPage'
          });
        }
      })
  
    },

    bindGetUserInfo: function(e) {
        if (e.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this;
          console.info("赠送"+that.data.giving);
          console.info("赠送" +that.data.usergiftcardid);
          console.info("赠送" +that.data.coupon);
          
            // // 获取到用户的信息了，打印到控制台上看下
            // console.log("用户的信息如下：");
            // console.log(e.detail.userInfo)
            wx.getStorageSync("status");
            wx.setStorageSync("userInfo", e.detail.userInfo);
            wx.login({
              success: function (res) {
                //微信js_code
                that.setData({ wxcode: res.code });
                //获取用户信息
                wx.getUserInfo({
                  success: function (res) {
                    //获取用户敏感数据密文和偏移向量
                    that.setData({ encryptedData: res.encryptedData });
                    that.setData({ iv: res.iv });
                    wx.request({
                      url: app.globalData.url+'getUserWXLoginInfo',
                      data:{
                        encryptedData: res.encryptedData,
                        iv: res.iv,
                        code: that.data.wxcode,
                        partnerUserId: that.data.partnerUserId,
                        cardId: that.data.usergiftcardid 
                      },
                      success: function (res) {
                        var data = res.data;
                        if (data.result==1) {
                          //wx.setStorageSync("data", data.data);
                          wx.setStorageSync("userId", data.data.userId);
                          wx.setStorageSync("nickName", data.data.nickName);
                          wx.setStorageSync("avatar", data.data.avatar);
                          wx.setStorageSync("applicationType", data.data.applicationType);
                          wx.setStorageSync("authStatus", data.data.authStatus);
                          wx.setStorageSync("balance", data.data.balance);
                          wx.setStorageSync("boundWechat", data.data.boundWechat);
                          wx.setStorageSync("commissionMoneyAmount", data.data.commissionMoneyAmount);
                          wx.setStorageSync("completeNumber", data.data.completeNumber);
                          wx.setStorageSync("createTime", data.data.createTime);
                          wx.setStorageSync("invitationCode", data.data.invitationCode);
                          wx.setStorageSync("inviteUserId", data.data.inviteUserId);
                          wx.setStorageSync("parentUserId", data.data.parentUserId);
                          wx.setStorageSync("partner", data.data.partner);
                          wx.setStorageSync("level", data.data.level);
                          wx.setStorageSync("partnerStatus", data.data.partnerStatus);
                          wx.setStorageSync("points", data.data.points);
                          wx.setStorageSync("promoteUserNumber", data.data.promoteUserNumber);
                          wx.setStorageSync("roleId", data.data.roleId);
                          wx.setStorageSync("sex", data.data.sex);
                          wx.setStorageSync("withdrawWay", data.data.withdrawWay);
                          wx.setStorageSync("wechatUnoinId", data.data.wechatUnoinId);
                          wx.setStorageSync("ongoing", data.data.ongoing);
                          wx.setStorageSync("registration", data.data.registration);
                          wx.setStorageSync("completed", data.data.completed);
                          wx.setStorageSync("token", data.data.token);
                          wx.setStorageSync("invitationPrice", data.data.invitationPrice);
                          wx.setStorageSync("receive", data.data.receive);
                          //wx.setStorageSync("pickUpCode", data.data.pickUpCode);
                          console.info("zhehsiqouquan" + data.data.token);
                         
                            if (that.data.assem == 1) {
                              wx.navigateTo({
                                url: '/pages/assembleDetail/assembleDetail?orderId=' + that.data.orderId + '&share=' + that.data.share + '&type=' + that.data.type + '&groupShoppingId=' + that.data.groupShoppingId+'&shareBack=' + that.data.shareBack,
                              })
                            } else if (that.data.productid != 0 && that.data.productid != undefined && that.data.productid != '') {
                              wx.navigateTo({
                                url: '/pages/productDetail/productDetail?productid=' + that.data.productid + '&type=' + that.data.type + '&back=1',
                              })

                          } else  if (that.data.giving == 1 || that.data.coupon == 1) { 
                              wx.setStorageSync("partnerUserId", that.data.partnerUserId);
                              wx.setStorageSync("giving", that.data.giving); 
                              wx.setStorageSync("usergiftcardid", that.data.usergiftcardid);
                              wx.setStorageSync("partnerUserName", that.data.partnerUserName);
                              console.info(that.data.partnerUserName);
                              wx.reLaunch({
                                url: '/pages/mallPage/mallPage'
                              });
                            } else if (that.data.giving == 2) {
                              wx.setStorageSync("partnerUserId", that.data.partnerUserId);
                              wx.setStorageSync("giving", that.data.giving);
                              wx.setStorageSync("cardid", that.data.cardid);
                              wx.setStorageSync("partnerUserName", that.data.partnerUserName);
                              wx.setStorageSync("price", that.data.price);
                              console.info(that.data.partnerUserName);
                              wx.reLaunch({
                                url: '/pages/mallPage/mallPage'
                              });
                            } else if (that.data.fan != undefined) {
                              wx.navigateBack({
                                
                              })
                            } else  {
                              
                              wx.reLaunch({
                                url: '/pages/mallPage/mallPage' 
                              });
                          }
                        } else {
                          wx.showToast({
                            title: res.data.errmsg,
                            icon: 'none'
                          })
                        }
                      }
                    })  
                  },
                  fail: function (res) {
                    wx.reLaunch({
                      url: '/pages/mallPage/mallPage'
                    });
                  }
                
                })
              }
            })
        } else {
          wx.reLaunch({
            url: '/pages/mallPage/mallPage'
          });

            //用户按了拒绝按钮
            // wx.showModal({
            //     title: '警告',
            //     content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
            //     showCancel: false,
            //     confirmText: '返回授权',
            //     success: function(res) {
            //         // 用户没有授权成功，不需要改变 isHide 的值
            //         if (res.confirm) {
            //             console.log('用户点击了“返回授权”');
                    
            //         }
            //     }
            // });
        }
    },
    navBack: function () {
      var that = this;
      console.info(that.data.back);
        wx.switchTab({
          url: '/pages/mallPage/mallPage'
        })
    }
})
